#include "CocosTipBox.h"

using namespace cocos2d;

CCocosTipBox::CCocosTipBox()
	: m_pSprBack(NULL)
{

}

CCocosTipBox *CCocosTipBox::CreateTipBox(BoxType type, const char *szName, float sizeOrScale, unsigned char alpha, bool bUseFrame, const char *szBoxTexture)
{
	ccColor4B color = {0, 0, 0, alpha};
	CCocosTipBox *pNewTipBox =  new CCocosTipBox();
	if(pNewTipBox && pNewTipBox->initWithColor(color))
	{
		pNewTipBox->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(pNewTipBox);
		return NULL;
	}
	
	pNewTipBox->InitState(type, szName, sizeOrScale, bUseFrame, szBoxTexture);

	return pNewTipBox;
}

void CCocosTipBox::InitState(BoxType type, const char *szName, float sizeOrScale,bool bUseFrame, const char *szBoxTexture)
{
	CCSize s = CCDirector::sharedDirector()->getWinSize();

	if(type == TYPE_BMFONT)
	{
		m_pLable = CCLabelBMFont::labelWithString("", szName);
		((CCLabelBMFont *)m_pLable)->setScale(sizeOrScale);
		((CCLabelBMFont *)m_pLable)->setPosition(ccp(s.width*0.5f, s.height*0.5f));
		addChild(((CCLabelBMFont *)m_pLable), 10);
		
	}
	else
	{
		m_pLable = CCLabelTTF::labelWithString("", szName, sizeOrScale);
		((CCLabelTTF *)m_pLable)->setPosition(ccp(s.width*0.5f, s.height*0.5f));
		addChild(((CCLabelTTF *)m_pLable), 10);
	}

	if(szBoxTexture != NULL)
	{
		if(bUseFrame)
		{
			m_pSprBack = CCSprite::spriteWithSpriteFrameName(szBoxTexture);
		}
		else
		{
			m_pSprBack = CCSprite::spriteWithFile(szBoxTexture);
		}

		m_pSprBack->setPosition(ccp(s.width*0.5f, s.height*0.5f));

		addChild(m_pSprBack, 0);
		
	}

	setIsVisible(false);
}


void CCocosTipBox::CancelWait()
{
	EnableControls(true);
	setIsVisible(false);
}

void CCocosTipBox::ShowTip(const char *szMsg, float waitTimer , cocos2d::CCAction *pAction)
{
	if(NULL != szMsg)
	{
		m_pLable->setString(szMsg);
	}

	EnableControls(false);
	setIsVisible(true);

	if(waitTimer > 0)
	{
		if(NULL == pAction)
		{
			runAction(CCSequence::actionOneTwo(CCDelayTime::actionWithDuration(waitTimer), 
				CCCallFunc::actionWithTarget(this, callfunc_selector(CCocosTipBox::OnTipFinish))));
		}
		else
		{
			runAction(CCSequence::actions(CCDelayTime::actionWithDuration(waitTimer), 
				CCCallFunc::actionWithTarget(this, callfunc_selector(CCocosTipBox::OnTipFinish)),
				pAction,
				NULL));
		}
	}
	
	
}

void CCocosTipBox::OnTipFinish()
{
	EnableControls(true);
	setIsVisible(false);
}

void CCocosTipBox::AddControl(CCocosUIBase *pControl)
{
	m_listControls.push_back(pControl);
}
void CCocosTipBox::RemoveControl(CCocosUIBase *pControl)
{
	m_listControls.remove(pControl);
	/*
	ControlList::iterator iter = find(m_listControls.begin(), m_listControls.end(), pControl);

	if(iter != m_listControls.end())
	{

	}
	*/
}

void CCocosTipBox::EnableControls(bool bEnable)
{
	for(ControlList::iterator iter = m_listControls.begin(); iter != m_listControls.end(); iter++)
	{
		((CCocosUIBase *)(*iter))->setEnable(bEnable);
	}
}